package com.ibo.htlivecamera.player;

import android.content.Context;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.VideoView;

import com.daniulive.smartplayer.SmartPlayerJniV2;
import com.eventhandle.NTSmartEventCallbackV2;
import com.eventhandle.NTSmartEventID;
import com.ibo.htlivecamera.R;
import com.ibo.htlivecamera.base.BaseFragment;
import com.ibo.htlivecamera.view.CustomVideoView;
import com.videoengine.NTRenderer;
import com.videoengine.NTUserDataCallback;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

public class RtspPlayerFragment extends BaseFragment implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnInfoListener {
    private CustomVideoView videoView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.frag_rtsp_player, container, false);
        return inflate;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        String playbackUrl = getArguments().getString("url");
        log("playbackUrl = " + playbackUrl);

        videoView = (CustomVideoView) view.findViewById(R.id.videoView);
        videoView.setVideoURI(Uri.parse(playbackUrl));
        videoView.setOnPreparedListener(this);
        videoView.setOnErrorListener(this);
        videoView.setOnCompletionListener(this);
        videoView.setOnInfoListener(this);

        videoView.start();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (videoView != null) {
            videoView.suspend();
        }
    }


    private void log(String s) {
        System.out.println("########   " + s);
    }


    @Override
    public void onPrepared(MediaPlayer mp) {
        System.out.println("###   RtspPlayerFragment.onPrepared");
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        System.out.println("###   RtspPlayerFragment.onError:" + what + "   " + extra);
        return false;
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        System.out.println("###   RtspPlayerFragment.onCompletion");

    }

    @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra) {
        System.out.println("###   RtspPlayerFragment.onInfo:" + what + "   " + extra);
        return false;
    }
}
